package com.qq.financeSystem.service.impl;


import com.qq.financeSystem.mapper.AnalyzeMapper;
import com.qq.financeSystem.mapper.OffsetMapper;
import com.qq.financeSystem.mapper.SendSalaryMapper;
import com.qq.financeSystem.model.*;
import com.qq.financeSystem.service.AnalyzeService;
import com.qq.financeSystem.utils.Parse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class AnalyzeServiceImpl implements AnalyzeService {

    @Autowired
    AnalyzeMapper analyzeMapper;
    @Autowired
    SendSalaryMapper sendSalaryMapper;
    @Autowired
    OffsetMapper offsetMapper;

    @Override
    public String queryMonthCountMoney() {
        BigDecimal weekCar = analyzeMapper.selectMonthCarMoney();
        BigDecimal weekFix = analyzeMapper.selectMonthFixMoney();
        BigDecimal money = weekFix.add(weekCar);
        //金额格式化，三位一个逗号
        return Parse.moneyParse(String.valueOf(money));
    }

    @Override
    public String queryYearCountMoney() {
        BigDecimal yearCar = analyzeMapper.selectYearCarMoney();
        BigDecimal yearFix = analyzeMapper.selectYearFixMoney();
        BigDecimal money = yearFix.add(yearCar);
        //金额格式化，三位一个逗号
        return Parse.moneyParse(String.valueOf(money));
    }

    @Override
    public String queryYearCarCount() {
        return analyzeMapper.selectYearCarCount();
    }

    @Override
    public String queryAllCourse() {
        return analyzeMapper.selectAllCourse();
    }

    @Override
    public String queryMonthAllOutMoney() {
        //金额格式化
        return Parse.moneyParse(analyzeMapper.selectMonthAllOutMoney());
    }

    @Override
    public String queryYearAllOutMoney() {
        //金额格式化
        return Parse.moneyParse(analyzeMapper.selectYearAllOutMoney());
    }

    @Override
    public String queryYearAllCar() {
        return analyzeMapper.selectYearAllCar();
    }

    @Override
    public String queryAllPerson() {
        return analyzeMapper.selectAllPerson();
    }

    @Override
    public List<String> queryWeekOneCarMoney() {
        //金额格式化，三位一个逗号(前端柱状图不能转换)
        return analyzeMapper.selectWeekOneCarMoney();
    }

    @Override
    public List<String> queryWeekOneFixMoney() {
        return analyzeMapper.selectWeekOneFixMoney();
    }

    @Override
    public List<MoneyModel> queryTenTwoByOneCarMoney() {
        return analyzeMapper.selectTenTwoByOneCarMoney();
    }

    @Override
    public List<MoneyModel> queryTenTwoByOneFixMoney() {
        return analyzeMapper.selectTenTwoByOneFixMoney();
    }

    @Override
    public List<MonthSalaryMoney> queryMonthSalaryOutMoney() {
        List<MonthSalaryMoney> list = new ArrayList<>();
        //销售部门+人数+底薪
        MonthSalaryMoney sellModel = sendSalaryMapper.selectSumSalaryMonthByIdent("1");
        //销售提成
        BigDecimal royalty = sendSalaryMapper.selectMonthAllRoyalty();
        sellModel.setRoyalty(royalty);
        //销售补贴
        BigDecimal sellOffsetMoney = offsetMapper.selectMonthOffsetMoneyByIdent("1");
        sellModel.setOffsetMoney(sellOffsetMoney);
        //销售部总额
        sellModel.setSumSalary(sellModel.getSalary().add(royalty).add(sellOffsetMoney));
        //售后部门+人数+底薪
        MonthSalaryMoney fixModel = sendSalaryMapper.selectSumSalaryMonthByIdent("3");
        //维修提成
        BigDecimal pushMoney = sendSalaryMapper.selectMonthAllPushMoney();
        fixModel.setRoyalty(pushMoney);
        //维修补贴
        BigDecimal fixOffsetMoney = offsetMapper.selectMonthOffsetMoneyByIdent("3");
        fixModel.setOffsetMoney(fixOffsetMoney);
        //售后部总额
        fixModel.setSumSalary(fixModel.getSalary().add(pushMoney).add(fixOffsetMoney));
        //仓库部门+人数+底薪
        MonthSalaryMoney homeModel = sendSalaryMapper.selectSumSalaryMonthByIdent("2");
        //仓库补贴
        BigDecimal homeOffsetMoney = offsetMapper.selectMonthOffsetMoneyByIdent("2");
        homeModel.setOffsetMoney(homeOffsetMoney);
        //仓库部总额
        homeModel.setSumSalary(homeModel.getSalary().add(homeOffsetMoney));
        //存入集合
        return Arrays.asList(sellModel, fixModel, homeModel);
    }

    @Override
    public List<MonthHomeMoney> queryMonthCarHomeOutMoney() {
        return sendSalaryMapper.selectMonthOutCarHome();
    }

    @Override
    public List<MonthHomeMoney> queryMonthPartHomeOutMoney() {
        return sendSalaryMapper.selectMonthOutPartsHome();
    }

    @Override
    public List<YearOutMoney> queryYearAllOutMoneyShow() {
        List<YearOutMoney> list = analyzeMapper.selectYearAllOutMoneyShow();
        //算本月合计金额
        for (YearOutMoney one:list) {
            one.setSumPrice(one.getSalary().add(one.getCarPrice()).add(one.getPartPrice()));
        }
        return list;
    }

    @Override
    public List<MonthSalaryMoney> queryOutYearToMonthByKey(YearAndMonth key) {
        ArrayList<MonthSalaryMoney> list = new ArrayList<>();
        String[] ss = {"1","3","2"};
        for (String s:ss) {
            key.setIdentificationCode(s);
            MonthSalaryMoney model = sendSalaryMapper.selectOutYearToMonthSalaryByKey(key);
            if (model.getDept() == null)
                continue;
            model.setSumSalary(model.getSalary().add(model.getRoyalty()).add(model.getOffsetMoney()));
            list.add(model);
        }
        return list;
    }

    @Override
    public List<MonthHomeMoney> queryOutYearToMonthPartHomeByKey(YearAndMonth key) {
        return sendSalaryMapper.selectOutYearToMonthPartByKey(key);
    }

    @Override
    public List<MonthHomeMoney> queryOutYearToMonthCarHomeByKey(YearAndMonth key) {
        return sendSalaryMapper.selectOutYearToMonthCarByKey(key);
    }

    @Override
    public List<MonthGetMoney> monthGetMoneyForCar() {
        return analyzeMapper.monthGetMoneyForCar();
    }

    @Override
    public List<MonthGetMoney> monthGetMoneyForFix() {
        return analyzeMapper.monthGetMoneyForFix();
    }
}
